<?php
// +----------------------------------------------------------------------
// | YBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.yhustc.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yhustc <yhustc@gmail.com>
// +----------------------------------------------------------------------
// $Id$

/**
 +------------------------------------------------------------------------------
 * 公共模块。之所以这些操作不放入BaseAction，因为其不需要BaseAction构造函数中的那些东西
 +------------------------------------------------------------------------------
 * @author    yhustc <yhustc@gmail.com>
 * @version   $Id$
 +------------------------------------------------------------------------------
 */

 class PublicAction extends Action
 {
 	/**
     +----------------------------------------------------------
     * 登录窗口
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
 	function _empty()
 	{
 		if(Session::is_set(C('USER_AUTH_KEY')))
 			$this->redirect("index","Index");
 		else
		{
			$this->assign('goto',empty($_GET['goto'])?__APP__:base64_decode($_GET['goto']));
 			$this->display("login");
		}
 	}
 	
	 /**
     +----------------------------------------------------------
     * 用户登出
     * 
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    function logout() 
    {
        if(Session::is_set(C('USER_AUTH_KEY'))) {
            
            Session::clear();
			
			$this->assign('jumpUrl',__APP__);
			$this->success("注销成功");
        }else {
            $this->error('已经注销！');
        }
        $this->forward();
    }


    /**
     +----------------------------------------------------------
     * 用户登录检查
     * 
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    function checkLogin() 
    {
		if(Session::get('verify')!=md5($_POST['verify'])){
			$this->error("验证码不正确！");
		}
		else
		{
			//验证码清除,防止反复提交
			$_SESSION["verify"] = time()+rand();

			// 生成认证条件
			// 这里使用用户名、密码和状态的方式进行认证
			$map = array();
			$map["user_login"] = $_POST['username'];
			$map["user_status"] = "1";
			$authInfo = D("User")->find($map);
			
			if(false === $authInfo || $authInfo["user_pass"] != md5($_POST['password'])) {
				$this->error("用户名或密码不正确！");
			}else {
				// 设置认证识别号
				Session::set(C('USER_AUTH_KEY'),$authInfo["ID"]);
				Session::set('loginid',$authInfo["user_login"]);

				// 登录成功
				$this->success("登录成功，马上跳转");
			}
		}
    }

	/**
	+----------------------------------------------------------
	* 验证码显示
	+----------------------------------------------------------
	* @access public 
	+----------------------------------------------------------
	* @return void
	+----------------------------------------------------------
	* @throws ThinkExecption
	+----------------------------------------------------------
	*/
    public function verify() 
    {
		$type =	isset($_GET['type'])?$_GET['type']:'gif';
        import("ORG.Util.Image");
        Image::buildImageVerify(4,1,$type);
    }
 }
 ?>